home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / util / mouse / FreeMouse.lha / FreeMouse / GUI.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-02  |  8.0 KB  |  332 lines

  1. #include <exec/libraries.h>
  2. #include <exec/memory.h>
  3. #include <graphics/text.h>
  4. #include <intuition/intuition.h>
  5. #include <intuition/screens.h>
  6. #include <intuition/GadgetClass.h>
  7. #include <libraries/gadtools.h>
  8. #include <utility/tagitem.h>
  9.  
  10. #include <clib/exec_protos.h>
  11. #include <clib/gadtools_protos.h>
  12. #include <clib/intuition_protos.h>
  13. #include <clib/graphics_protos.h>
  14.  
  15. #include "Cx.h"
  16. #include "Icon.h"
  17.  
  18. #include "GUI.h"
  19.  
  20. #define Speed_ID 1
  21. #define ClickToFront_ID 2
  22. #define MMBShift_ID 3
  23. #define Quit_ID 127
  24.  
  25. struct TextAttr GUI_TopazFont = { "topaz.font", 8, 0, 0, };
  26.  
  27. extern struct CxContext *Cx;
  28.  
  29. struct Gadget *BuildQuitButton(struct GUIContext *gui,struct Gadget *glist)
  30. {
  31.   struct NewGadget newgad;
  32.   int topedge,leftedge,textwidth,textwidth2,id=Quit_ID;
  33.  
  34.   textwidth=TextLength(&gui->Screen->RastPort,"Quit",4);
  35.   textwidth+=64;
  36.  
  37.   topedge=gui->BorderTop+gui->InnerHeight;
  38.   leftedge=gui->BorderLeft+gui->InnerWidth/2-textwidth/2;
  39.   gui->InnerHeight+=8+14;
  40.  
  41.   newgad.ng_TextAttr   = &GUI_TopazFont;
  42.   newgad.ng_VisualInfo = gui->VisualInfo;
  43.   newgad.ng_LeftEdge   = leftedge;
  44.   newgad.ng_TopEdge    = topedge;
  45.   newgad.ng_Width      = textwidth;
  46.   newgad.ng_Height     = 14;
  47.   newgad.ng_GadgetText = "Quit";
  48.   newgad.ng_GadgetID   = id;
  49.   newgad.ng_Flags      = 0;
  50.  
  51.   glist = CreateGadget(BUTTON_KIND, glist, &newgad,
  52.                        GA_RelVerify,TRUE,
  53.                        TAG_DONE);
  54.  
  55.   return(glist);
  56. }
  57.  
  58.  
  59. struct Gadget *BuildCheckBoxes(struct GUIContext *gui,struct Gadget *glist)
  60. {
  61.   struct NewGadget newgad;
  62.   int topedge,leftedge,textwidth,textwidth2;
  63.  
  64.   BOOL ctf,mmb;
  65.  
  66.   if(Cx)
  67.   {
  68.     ctf=Cx->ClickToFront;
  69.     mmb=Cx->MMBShift;
  70.   }
  71.   else
  72.   {
  73.     ctf=MatchToolType("ClickToFront","Yes");
  74.     mmb=MatchToolType("MMBShift","Yes");
  75.   }
  76.  
  77.   textwidth=TextLength(&gui->Screen->RastPort,"Click to Front",14);
  78.   textwidth+=64;
  79.  
  80.   topedge=gui->BorderTop+gui->InnerHeight;
  81.   leftedge=gui->BorderLeft;
  82.   gui->InnerHeight+=8+14;
  83.  
  84.   newgad.ng_TextAttr   = &GUI_TopazFont;
  85.   newgad.ng_VisualInfo = gui->VisualInfo;
  86.   newgad.ng_LeftEdge   = leftedge+textwidth;
  87.   newgad.ng_TopEdge    = topedge;
  88.   newgad.ng_Width      = 14;
  89.   newgad.ng_Height     = 14;
  90.   newgad.ng_GadgetText = "Click to Front";
  91.   newgad.ng_GadgetID   = ClickToFront_ID;
  92.   newgad.ng_Flags      = 0;
  93.  
  94.   glist = CreateGadget(CHECKBOX_KIND, glist, &newgad,
  95.                        GTCB_Checked,ctf,
  96.                        GA_RelVerify,TRUE,
  97.                        TAG_DONE);
  98.   if(!glist)
  99.     return(NULL);
  100.  
  101.   topedge=gui->BorderTop+gui->InnerHeight;
  102.   gui->InnerHeight+=8+14;
  103.  
  104.   newgad.ng_TextAttr   = &GUI_TopazFont;
  105.   newgad.ng_VisualInfo = gui->VisualInfo;
  106.   newgad.ng_LeftEdge   = leftedge+textwidth;
  107.   newgad.ng_TopEdge    = topedge;
  108.   newgad.ng_Width      = 14;
  109.   newgad.ng_Height     = 14;
  110.   newgad.ng_GadgetText = "MMB Shift";
  111.   newgad.ng_GadgetID   = MMBShift_ID;
  112.   newgad.ng_Flags      = 0;
  113.  
  114.   glist = CreateGadget(CHECKBOX_KIND, glist, &newgad,
  115.                        GTCB_Checked,mmb,
  116.                        GA_RelVerify,TRUE,
  117.                        TAG_DONE);
  118.  
  119.   return(glist);
  120. }
  121.  
  122.  
  123. struct Gadget *BuildSliders(struct GUIContext *gui,struct Gadget *glist)
  124. {
  125.   struct NewGadget newgad;
  126.   int topedge,leftedge,textwidth,textwidth2,level,id=0;
  127.  
  128.   if(Cx)
  129.     level=Cx->MouseSpeed;
  130.   else
  131.     level=(GetNumericToolType("MouseSpeed",100));
  132.  
  133.   textwidth=TextLength(&gui->Screen->RastPort,"Speed: 300%",11);
  134.  
  135.   /* The offsets of our window borders */
  136.   gui->BorderLeft = gui->Screen->WBorLeft;
  137.   gui->BorderTop = gui->Screen->WBorTop + (gui->Screen->Font->ta_YSize + 1);
  138.  
  139.   topedge=8+gui->BorderTop;
  140.   leftedge=16;
  141.   gui->InnerHeight+=30;
  142.   gui->InnerWidth=28;
  143.   textwidth+=20;
  144.  
  145.   /* Setup our first gadget */
  146.   newgad.ng_TextAttr   = &GUI_TopazFont;
  147.   newgad.ng_VisualInfo = gui->VisualInfo;
  148.   newgad.ng_LeftEdge   = leftedge+textwidth;
  149.   newgad.ng_TopEdge    = topedge;
  150.   newgad.ng_Width      = 140;
  151.   newgad.ng_Height     = 16;
  152.   newgad.ng_GadgetText = NULL;
  153.   newgad.ng_GadgetID   = 1;
  154.   newgad.ng_Flags      = PLACETEXT_LEFT;
  155.   /* Now create it and add it to our list */
  156.   glist = CreateGadget(SLIDER_KIND, glist, &newgad,
  157.                        GA_RelVerify,TRUE,
  158.                        GTSL_LevelFormat,"Speed: %ld%%",
  159.                        GTSL_MaxLevelLen,12,
  160.                        GTSL_Min,30,
  161.                        GTSL_Max,300,
  162.                        GTSL_Level,level,
  163.                        PGA_Freedom,LORIENT_HORIZ,
  164.                        TAG_DONE);
  165.   if(!glist)
  166.     return(NULL);
  167.  
  168.   gui->InnerWidth+=textwidth+140;
  169.   leftedge+=textwidth;
  170.   return(glist);
  171. }
  172.  
  173.  
  174. void DisposeGUI(struct GUIContext *gui)
  175. {
  176.   if(gui->Window)
  177.     gui->Hide(gui);
  178.  
  179.   FreeVec(gui);
  180. }
  181.  
  182.  
  183. char *Window_Open(struct GUIContext *gui)
  184. {
  185.   struct Gadget *glist = NULL;
  186.  
  187.   if(gui->Window)
  188.     return("Window already open!");
  189.  
  190.   gui->InnerWidth=0;
  191.   gui->InnerHeight=0;
  192.  
  193.   if(!(gui->Screen=LockPubScreen(NULL)))
  194.     return(NULL);
  195.  
  196.   if(!(gui->VisualInfo = GetVisualInfo(gui->Screen, TAG_DONE)))
  197.     return(NULL);
  198.  
  199.   if(!(gui->ContextGadget = CreateContext(&glist)))
  200.     return(NULL);
  201.  
  202.   if(!(glist=BuildSliders(gui,glist)))
  203.     return(NULL);
  204.  
  205.   if(!(glist=BuildCheckBoxes(gui,glist)))
  206.     return(NULL);
  207.  
  208.   if(!(glist=BuildQuitButton(gui,glist)))
  209.     return(NULL);
  210.  
  211.   if(gui->Window = OpenWindowTags(NULL,
  212.                           WA_Activate,TRUE,
  213.                           WA_InnerWidth,gui->InnerWidth,
  214.                           WA_InnerHeight,gui->InnerHeight,
  215.                           WA_Flags,WFLG_CLOSEGADGET | WFLG_DEPTHGADGET | WFLG_DRAGBAR,
  216.                           WA_IDCMP,SLIDERIDCMP | IDCMP_GADGETUP | IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW,
  217.                           WA_Gadgets,gui->ContextGadget,
  218.                           WA_Title,"FreeMouse V1.0",
  219.                           TAG_DONE,0))
  220.   {
  221.     GT_RefreshWindow(gui->Window, NULL);
  222.     gui->Signals|=(1<<gui->Window->UserPort->mp_SigBit);
  223.     return(NULL);
  224.   }
  225.   else
  226.     return("Error: could not open window\n");
  227. }
  228.  
  229.  
  230. void Window_Close(struct GUIContext *gui)
  231. {
  232.   if(gui->Window)
  233.   {
  234.     gui->Signals^=(1<<gui->Window->UserPort->mp_SigBit);
  235.     CloseWindow(gui->Window);
  236.     gui->Window=NULL;
  237.   }
  238.  
  239.   if(gui->ContextGadget)
  240.   {
  241.     FreeGadgets(gui->ContextGadget);
  242.     gui->ContextGadget=NULL;
  243.   }
  244.  
  245.   if(gui->VisualInfo)
  246.   {
  247.     FreeVisualInfo(gui->VisualInfo);
  248.     gui->VisualInfo=NULL;
  249.   }
  250.  
  251.   if(gui->Screen)
  252.   {
  253.     UnlockPubScreen(NULL, gui->Screen);
  254.     gui->Screen=NULL;
  255.   }
  256. }
  257.  
  258.  
  259. BOOL HandleIDCMP(struct GUIContext *gui,unsigned long signals)
  260. {
  261.   struct Window *win=gui->Window;
  262.   BOOL cont=TRUE,hide=FALSE;
  263.   long level;
  264.   struct Gadget *gadget;
  265.   if(signals&gui->Signals)
  266.   {
  267.     if(win)
  268.     {
  269.       struct IntuiMessage* intuimsg;
  270.       while(intuimsg = GT_GetIMsg(win->UserPort))
  271.       {
  272.         switch(intuimsg->Class)
  273.         {
  274.         case IDCMP_MOUSEMOVE:
  275.         case IDCMP_GADGETUP:
  276.           if(gadget=(struct Gadget *)intuimsg->IAddress)
  277.           {
  278.             switch(gadget->GadgetID)
  279.             {
  280.               case Speed_ID:
  281.                 GT_GetGadgetAttrs(gadget,win,NULL,GTSL_Level,&level,TAG_DONE);
  282.                 Cx->MouseSpeed=level;
  283.                 break;
  284.               case ClickToFront_ID:
  285.                 GT_GetGadgetAttrs(gadget,win,NULL,GTCB_Checked,&level,TAG_DONE);
  286.                 Cx->ClickToFront=level;
  287.                 break;
  288.               case MMBShift_ID:
  289.                 GT_GetGadgetAttrs(gadget,win,NULL,GTCB_Checked,&level,TAG_DONE);
  290.                 Cx->MMBShift=level;
  291.                 break;
  292.               case Quit_ID:
  293.                 cont=FALSE;
  294.                 break;
  295.             }
  296.           }
  297.           break;
  298.         case IDCMP_CLOSEWINDOW:
  299.           hide=TRUE;
  300.           break;
  301.         case IDCMP_REFRESHWINDOW:
  302.           GT_BeginRefresh(win);
  303.           GT_EndRefresh(win, TRUE);
  304.           break;
  305.         }
  306.         GT_ReplyIMsg(intuimsg);
  307.       }
  308.       if(hide)
  309.         gui->Hide(gui);
  310.       return(cont);
  311.     }
  312.   }
  313.   return(TRUE);
  314. }
  315.  
  316.  
  317. struct GUIContext *CreateGUI()
  318. {
  319.   struct GUIContext *gui;
  320.  
  321.   if(!(gui=AllocVec(sizeof(struct GUIContext),MEMF_CLEAR)))
  322.     return(NULL);
  323.  
  324.   gui->Dispose=DisposeGUI;
  325.   gui->Show=Window_Open;
  326.   gui->Hide=Window_Close;
  327.   gui->Handle=HandleIDCMP;
  328.  
  329.   return(gui);
  330. }
  331.  
  332.